﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Entities;
using Utility;
using BUS;

public partial class Administrator_voucher : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            load_data();
        }
    }
    private void load_data()
    {
        Literal1.Text = "Danh sách voucher";
        pager.PageSize = 15;
        Criteria c = new Criteria();
        int total = 0;
        try
        {
            string dadung = Request.QueryString["used"].ToString();
            if (dadung == "true")
            {
                c.add(Condition.AND, Tbl_Voucher_Columns.Used, Condition.EQUAL, 1);
            }
            else if (dadung == "false")
            {
                c.add(Condition.AND, Tbl_Voucher_Columns.Used, Condition.EQUAL, 0);
            }
        }
        catch { }
       

        Tbl_Voucher_Collection vcCollection =
               Tbl_Voucher_BUS.Get(c.Criter, pager.CurrentIndex, pager.PageSize,
               Tbl_Voucher_Columns.STT.ToString(), OrderDirection.ASC.ToString(), out total);
        Repeater1.DataSource = vcCollection;
        Repeater1.DataBind();
    }

    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem ||
                  e.Item.ItemType == ListItemType.Item)
        {
            Tbl_Voucher_Entities vc = (Tbl_Voucher_Entities)e.Item.DataItem;
            Tbl_DonHang_Entities dh = Tbl_DonHang_BUS.GetByID(vc.DonHang);
            Literal lit_serial = (Literal)e.Item.FindControl("lit_serial");
            HyperLink hyp_madh = (HyperLink)e.Item.FindControl("hyp_madh");
            Literal lit_stt = (Literal)e.Item.FindControl("lit_stt");
            CheckBox chkb_dasudung = (CheckBox)e.Item.FindControl("chkb_dasudung");

            lit_stt.Text = vc.STT.ToString();

            if (vc.Used == true)
            {
                chkb_dasudung.Checked = true;
                chkb_dasudung.Enabled = false;
            }
            else
            {
                chkb_dasudung.Checked = false;
                chkb_dasudung.Enabled = true;
            }
            lit_serial.Text = vc.Serial.ToString();
            hyp_madh.NavigateUrl = "~/Administrator/dathang_chitiet.aspx?id=" + vc.DonHang.ToString();
            hyp_madh.Text = dh.MaDH.ToString();
        }
    }
    protected void bt_update_Click(object sender, EventArgs e)
    {
        for(int i=0; i< Repeater1.Items.Count ;i++)
        {
            CheckBox chkb_dasudung = (CheckBox)Repeater1.Items[0].FindControl("chkb_dasudung");
            if (chkb_dasudung.Enabled == true)
            {
                if (chkb_dasudung.Checked == true)
                {
                    Tbl_Voucher_Entities vc = new Tbl_Voucher_Entities();
                    Literal lit_serial = (Literal)Repeater1.Items[i].FindControl("lit_serial");
                    HyperLink hyp_madh = (HyperLink)Repeater1.Items[i].FindControl("hyp_madh");
                    Tbl_DonHang_Entities dh = Tbl_DonHang_BUS.GetByMADH(hyp_madh.Text);
                    Literal lit_stt = (Literal)Repeater1.Items[i].FindControl("lit_stt");

                    vc.STT = int.Parse(lit_stt.Text);
                    vc.DonHang = dh.STT;
                    vc.Serial = lit_serial.Text;
                    vc.Used = true;
                    Tbl_Voucher_BUS.Update(vc);
                    Response.Redirect("~/Administrator/voucher.aspx");
                }
            }
        }
    }
}
